package com.controller;

import com.mapper.BranchMapper;
import com.mapper.DepartmentMapper;
import com.mapper.HospitalMapper;
import com.mapper.RoleMapper;
import com.pojo.*;
import com.service.UserService;
import com.util.Pager;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/sys/user")
public class UserHandler {
    @Resource
    private UserService userService;


    /**
     * 分页查询
     * @param user
     * @param limit
     * @param offset
     * @return
     * @throws UnsupportedEncodingException
     */
    @RequestMapping("/searchPager")
    public Pager<User> searchPager(User user,Integer limit,Integer offset) throws UnsupportedEncodingException {
        if(user.getUserName()!=null){
            URLDecoder.decode(user.getUserName(),"UTF-8");
        }
        if(user.getLoginName()!=null) {
            URLDecoder.decode(user.getLoginName(), "UTF-8");
        }
        Pager<User> pager =new Pager<>();
        pager.setLimit(limit);
        pager.setOffSet(offset);
        pager.setCondition(user);
        return userService.searchUserPager(pager);
    }

    /**
     * 删除单个
     * @param uid
     * @return int
     */
    @RequestMapping("/delUser")
    public String deleteUser(Integer uid){
        String msg="删除失败！";
        int i =userService.deleteUser(uid);
        if(i>0){
            msg="删除成功！";
        }
        return msg;
    }

    /**
     * 修改
     * @param user
     * @return
     */
    @RequestMapping("/updateUser")
    public String updateUser(User user){
        String msg="修改失败！";
        int i = userService.update(user);
        if(i>0){
            msg="修改成功！";
        }
        return msg;
    }

    /**
     * 添加
     * @param user
     * @param session
     * @return
     */
    @RequestMapping("/addUser")
    public String addUser(User user, HttpSession session){
       /* User u = (User) session.getAttribute("loginUser");
        user.setCreateBy(u.getUserName());
        user.setCreateDate(new Date());*/
        String msg="添加失败！";
        int i = userService.addUser(user);
        if(i>0){
            msg="添加成功！";
        }
        return msg;
    }

}
